package cool.taomu.demo.entity.sina

import java.util.List
import org.apache.oro.text.perl.Perl5Util
import org.apache.oro.text.regex.PatternMatcherInput
import org.jsoup.Connection.Method
import org.jsoup.Jsoup

class SinaStock {
	def static getStockData(String code) {
		try {
			var String url = String.format("http://hq.sinajs.cn/list=%s", code);
			var connect = Jsoup.connect(url).referrer("http://finance.sina.com.cn");
			var respone = connect.maxBodySize(50000000).method(Method.GET).ignoreContentType(true).execute();
			var body = respone.body;
			var p5 = new Perl5Util();
			var input = new PatternMatcherInput(body);
			var List<StockMarketEntity> smelist = newArrayList();
			while (p5.match("/var hq_str_(sh|sz)([0-9]{6})=\"(.*?)\";/", input)) {
				val scode = p5.group(2);
				var r = p5.group(3).split(",");
				var jrkpj = Float.parseFloat(r.get(1));
				if (jrkpj != 0) {
					var name = r.get(0);
					var zrspj = Float.parseFloat(r.get(2));
					var dqjg = Float.parseFloat(r.get(3));
					var jrzgj = Float.parseFloat(r.get(4));
					var jrzdj = Float.parseFloat(r.get(5));
					var sjgs = Float.parseFloat(r.get(8));
					var sjje = Float.parseFloat(r.get(9));
					var sme = new StockMarketEntity(scode, name, jrkpj, zrspj, dqjg, jrzgj, jrzdj, sjgs, sjje);
					sme.date = r.get(30);
					sme.time = r.get(31);
					smelist.add = sme;
				}
			}
			return smelist;
		} catch (Exception ex) {
			ex.printStackTrace
		}
	}
}
